using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace GMailNotifierNET.Classes.BrowserInformation
{
    public class BrowserIcon
    {
        [DllImport("shell32.dll")]
        static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);


        private Icon m_icon = null;
        private string m_sIconFilename = null;
        private int m_sIconIndex = 0;

        public BrowserIcon(string fullIconPath)
        {
            string[] splittedPath = fullIconPath.Split(',');

            this.m_sIconFilename = string.Join(",", splittedPath, 0, splittedPath.Length - 1);
            this.m_sIconIndex = int.Parse(splittedPath[splittedPath.Length - 1]);
            this.m_icon = this.GetImageFromExecutable(
                this.m_sIconFilename,
                this.m_sIconIndex);
        }

        private Icon GetImageFromExecutable(string iconPath, int iconNumber)
        {
            IntPtr ptrImage = ExtractIcon(
                Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]),
                iconPath,
                iconNumber);
            
            return Icon.FromHandle(ptrImage);
        }

        public Icon Icon
        {
            get { return this.m_icon; }
        }

        public string IconFilename
        {
            get { return this.m_sIconFilename; }
        }

        public int IconIndex
        {
            get { return this.m_sIconIndex; }
        }
    }
}
